var liveTable;
var roleid = document.getElementById("roleid").value;
if (roleid == "0") {
	document.getElementById("actions").style.visibility = "hidden";// 隐藏
}
function getliveList() {
	if (typeof liveTable !== 'undefined') {
		liveTable.fnPageChange('first');
		return;
	}
	var i = 1;
	liveTable = $("#liveList")
			.DataTable(
					{
						"sAjaxSource" : _path + "/live/queryByPage",
						"fnServerParams" : function(aoData) {
							aoData.push({
								"name" : "name",
								"value" : $("#name").val()
							}, {
								"name" : "status",
								"value" : $("#status").val()
							}, {
								"name" : "fromDate",
								"value" : $("#fromDate").val()
							}, {
								"name" : "toDate",
								"value" : $("#toDate").val()
							});
						},
						"sServerMethod" : "POST",
						"sAjaxDataProp" : "dataList",
						"fnCreatedRow" : function() {
							i++;
						},
						"fnPreDrawCallback" : function() {
							i = 1;
						},
						"aoColumns" : [
								{
									"sTitle" : "序号",
									"sWidth" : "2%",
									"mData" : function() {
										return i;
									}
								},
								{
									"sTitle" : "直播ID",
									"mData" : "id",
									"sWidth" : "6%"
								},
								{
									"sTitle" : "直播名称",
									"sWidth" : "10%",
									"mData" : function(data) {
										var name = '<a href="' + _path
												+ '/live/edit/' + data.id
												+ '" >' + data.name + '</a>';
										return name;
									}
								},
								{
									"sTitle" : "线路名",
									"mData" : "mainname",
									"sWidth" : "10%"
								},
								{
									"sTitle" : "线路状态",
									"mData" : "status",
									"sWidth" : "10%",
									"mRender" : function(data) {
										var status = '启动中';

										return status;
									}
								},
								{
									"sTitle" : "播放状态",
									"mData" : "status",
									"sWidth" : "10%",
									"mRender" : function(data) {
										var status = '';
										switch (data) {
										case '0':
											status = '播放片花';
											break;
										case '1':
											status = '播放直播';
											break;
										case '2':
											status = '播放回播';
											break;
										default:
											status = '未知';
											break;
										}
										return status;
									}
								},
								{
									"sTitle" : "发布时间",
									"mData" : "createTime",
									"sWidth" : "10%"
								},
								{
									"sTitle" : "操作员",
									"mData" : "user.username",
									"sWidth" : "10%"
								},
								{
									"sTitle" : "操作",
									"sWidth" : "10%",
									"mData" : function(data) {

										var status = '<a href="'
												+ _path
												+ '/live/edit/'
												+ data.id
												+ '" class="btn default btn-xs purple"><i class="fa fa-edit" title="编辑"></i></a>';

										status += '&#8197<select   cti="'
												+ data.name
												+ '"    id="select'
												+ data.id
												+ '" name="select" onchange='
												+ "advertising("
												+ data.id
												+ ",'"
												+ data.mainid
												+ "','"
												+ data.backid
												+ "',this)"
												+ ' style="height:21px" class="btn default btn-xs purple">'
												+ '<option value="" selected="selected">操作</option>'
												+ '<option value="8">页签设置</option>'
												+ '<option value="0">播放片花</option>'
												+ '<option value="1">播放直播</option>'
												+ '<option value="2">播放回放</option>'
												+ '<option value="3">直播合作广告管理</option>'
												+ '<option value="4">直播交流广告管理</option>'
												+ '<option value="5">直播相关视频管理</option>'
												+ '<option value="6">导播地址</option>'
												+ '<option value="7">直播地址</option>'
												+ '<option value="9">直播二维码</option>'
												+ '<option value="13">推流二维码</option>'
												+ '<option value="10">直播ppt管理</option>'
												+ '<option value="11">直播统计</option>'
												+ '<option value="12">直播交流管理</option>'
												+ '<option value="14">投票管理</option>'
												+ '<option value="15">付费直播支付记录</option>'
												+ '<option value="16">打赏记录</option>'
												+ '<option value="19">红包记录</option>'
												+ '<option value="17">秘钥管理</option>'
												+ '<option value="18">审核授权管理</option>'
												+ '</select>';
										status += '&nbsp;<a href="javascript:inquiry('
												+ data.id
												+ ');"  class="btn default btn-xs red"><i class="fa fa-trash-o" title="删除"></i></a>';
										// if (roleid == "0") {

										// }
										return status;
									}
								} ]
					});
}
function inquiry(id) {

	layer.confirm('这将删除所有相关信息！您确定要删除吗？', {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		location.href = _path + "/live/del/" + id;
	}, function() {

	});
}
function evals(s) {
	return new Object();
};
// 下拉列表选项操作
function advertising(id, mainid, backid, em) {
	var name = $(em).attr("cti");
	var i = $("#select" + id).val(); // 获取选中的value的值
	var s = $("#select" + id).find("option:selected").text(); // 获取选中文本域的值
	if (i == '0' || i == '1' || i == '2') {
		$.ajax({
			type : "POST",
			url : _path + "/live/statusSwitch?id=" + id + "&status=" + i,
			async : false,
			dataType : "json",
			error : function(request) {
				alert("连接错误！");
			},
			success : function(data) {
				var chat = data.obj;
				$.ajax({
					type : "POST",
					url : "/w/guide/editLievzt?id=" + chat.id + "&lstatus="
							+ chat.status + "" + "" + "&liveurl="
							+ chat.liveurl + "&uname=" + chat.moviurl + "&ups="
							+ chat.backmoviurl + "",
					async : false,
					dataType : "json",
					error : function(request) {
						layer.alert("连接错误！");
						Refresh();
					},
					success : function(data) {
						layer.alert("状态切换成功");
						Refresh();
						return;
					}
				});

			}

		});
	} else if (i == '3') {
		location.href = _path + "/communication/list/" + id;
	} else if (i == '4') {
		location.href = _path + "/advertising/list/" + id;
	} else if (i == '5') {
		location.href = _path + "/relatedVideo/list?mainid=" + mainid
				+ "&liveid=" + id;
		// $.ajax({
		// cache : true,
		// type : "POST",
		// url : _path + "/relatedVideo/list?mainid="+mainid+"&backid="+backid,
		// async : false,
		// dataType : "json",
		// error : function(request) {
		// layer.alert("获取视频接口的'app'参数错误", {
		// icon : 2,
		// offset : 'middle',
		// time : 3000,
		// skin : 'layui-layer-lan'
		// });
		// // setTimeout("Refreshs()",1000);
		// },
		// success : function(jsondata) {
		// if (jsondata.ok) {
		// layer.alert(jsondata.ok, {
		// icon : 1,
		// offset : 'middle',
		// time : 3000,
		// skin : 'layui-layer-lan'
		// });
		// setTimeout("Refreshs('" + mainid + "','"+backid+"')", 1000);
		//
		// return true;
		// } else if (jsondata.sy) {
		// /*layer.alert(jsondata.sy, {
		// icon : 2,
		// offset : 'middle',
		// time : 3000,
		// skin : 'layui-layer-lan'
		// });*/
		// setTimeout("Refreshs('" + mainid + "','"+backid+"')");
		// return true;
		// }/*else {
		// layer.alert(jsondata.no, {
		// icon : 2,
		// offset : 'middle',
		// time : 3000,
		// skin : 'layui-layer-lan'
		// });
		// setTimeout("Refreshs('" + mainid + "','"+backid+"')", 1000);
		// return true;
		// }*/
		// }
		// });
	} else if (i == '6') {
		$.ajax({
			cache : true,
			type : "POST",
			url : _path + "/live/directorUrl?liveId=" + id + "&status=1",
			async : false,
			dataType : "json",
			error : function(request) {
				alert("出现错误");
			},
			success : function(jsondata) {
				if (jsondata.url) {
					$("#daoliveurl").val(jsondata.url);
					if (jsondata.status == '1') {
						$("#dlid").val(name);
					}
					$('#dbajax-modal').modal('show');
				}
			}
		});

	} else if (i == '7') {
		// location.href = "/lives/liveindex/"+ id;
		var urls=isflopen(id);
		if(urls!='0'){
			var ym=window.location.host;
			$("#liveurl").val('http://'+ym+'/lives/liveindex/'+id);
			$("#lid").val(name);
			$("#wapurl").val(urls);
			$('#ajax-modal').modal('show');
		}else{
		$.ajax({
			cache : true,
			type : "POST",
			url : _path + "/live/directorUrl?liveId=" + id + "&status=2",
			async : false,
			dataType : "json",
			error : function(request) {
				alert("出现错误");
			},
			success : function(jsondata) {
				if (jsondata.url) {
					var ym=window.location.host;
					$("#liveurl").val('http://'+ym+'/lives/liveindex/'+id);
				
					$("#wapurl").val('http://'+ym+'/w/live/index/'+id);
					if (jsondata.status == '2') {
						$("#lid").val(name);
					}
				}
				$('#ajax-modal').modal('show');
			}
		});
}
		// ReleasePop()
	} else if (i == '8') {
		location.href = _path + "/label/listin?liveid=" + id;
	} else if (i == '9') {
		var urls=isflopen(id);
		if(urls!='0'){
			var str1 = urls.replace(/&/g, ']');
			$("#imgs").attr('src',
					_path + "/createqcode/qcodezhfl?url="+str1);
			ReleasePops(); // 该方法在页面
		}else{
		$.ajax({
			url : _path + "/createqcode/directorUrl?liveId=" + id,
			async : false,
			success : function(data) {
				var url = data.url;
				if (url != null && url != "") {
					$("#imgs").attr('src',
							_path + "/createqcode/qcode?url=" + url);
					ReleasePops(); // 该方法在页面
				}
			}
		});
		}
	} else if (i == '13') {
		
		var url="http://www.bee-live.cn/jk-bin/live/edit?liveId="+id;
					$("#imgs").attr('src',
							_path + "/createqcode/qcode?url=" + url);
					ReleasePops(); // 该方法在页面
				
	
	} else if (i == '10') {
		//location.href = _path + "/pptImg/edit/" + id;
		location.href = _path + "/pptList/list/?liveId=" + id;
	} else if (i == '11') {

		location.href = _path + "/live/countLive?id=" + id;
	}else if (i == '12') {
//评论
		location.href = _path + "/chat/list?liveId=" + id;
	}else if (i == '14') {
//投票
		location.href = _path + "/evaluationProject/list?liveId=" + id;
	}else if (i == '15') {
//付费直播记录
		location.href = _path + "/redPacket/list?liveId=" + id+"&rpType=3";
	}else if (i == '16') {
//打赏记录
		location.href = _path + "/redPacket/list?liveId=" + id+"&rpType=4";
	}else if (i == '17') {
//秘钥记录
		location.href = _path + "/secretkeyl/list?liveId=" + id ;
	}else if (i == '18') {
//审核授权管理
		location.href = _path + "/checkLive/list?liveId=" + id ;
	}else if (i == '19') {
//红包记录
		location.href = _path + "/redPacket/list?liveId=" + id+"&rpType=1";	}
}

/*
 * function cklive(id){ var i=$("#liveId"+id).val(); var
 * s=$("#liveId"+id).find("option:selected").text(); //alert(s) $.ajax({ cache:
 * true, type: "POST", url:_path+"/live/statusSwitch?id="+id+"&status="+i,
 * async: false, dataType : "json", error: function(request) { alert("连接错误！"); },
 * success: function(data) { layer.alert("已切换为:"+s, { icon: 1, offset: 'middle',
 * time: 2000, skin: 'layui-layer-lan'
 * //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅 }); setTimeout("Refresh()",2000); } }) }
 */

// 对当前页面进行刷新
function Refresh() {
	location.reload();
}
// 页面重定向跳转
function Refreshs(mainid, backid) {
	location.href = _path + "/relatedVideo/list/?mainid=" + mainid + "&backid="
			+ backid;
}
// 获取主题
function queryAllKindergarten(id) {
	$.ajax({
		url : _path + "/live/queryAllKindergarten?id=",
		type : "POST",
		dataType : "json",
		async : false,
		success : function(jsonMap) {
			var list = jsonMap.dataList;
			$("#theme").html("");
			$("#theme").append("<option value='' >请选择</option>");
			if (list.length > 0) {
				for (var i = 0; i < list.length; i++) {
					var k = list[i];
					var op = "";
					if (id == k.id) {
						op = "<option value=" + k.id + " selected='selected'>"
								+ k.value0 + "</option>";
					} else {
						op = "<option value=" + k.id + " >" + k.value0
								+ "</option>";
					}
					$("#theme").append(op);
				}
			}
		}
	});
}
// 获取模版
function queryAllInterestClass(id) {
	var r = document.getElementById("theme").value;
	$.ajax({
		url : _path + "/live/queryAllInterestClass?kId=" + r,
		type : "POST",
		dataType : "json",
		async : false,
		success : function(jsonMap) {
			var list = jsonMap.intList;
			$("#template").html("");
			$("#template").append("<option selected value=''>请选择</option>");
			if (list.length > 0) {
				for (var i = 0; i < list.length; i++) {
					var interest = list[i];
					var op = "";
					var im = "";
					if (id == interest.id) {
						op = "<option value=" + interest.id
								+ " selected='selected'>" + interest.value0
								+ "</option>";
						im = "<img src=" + interest.value2
								+ " style='height: 420px;width: 240px;'>";
					} else {
						op = "<option value=" + interest.id + " >"
								+ interest.value0 + "</option>";
					}
					$("#template").append(op);
					$("#im").append(im);
				}
			}

		}
	});
}

function queryAllInterestClass1(id) {
	var r = document.getElementById("template").value;

	$.ajax({
		url : _path + "/live/queryAllInterestClass1?id=" + r,
		type : "POST",
		dataType : "json",
		async : false,
		success : function(jsonMap) {
			$("#im").html("");
			var list = jsonMap.intList;
			if (list.length > 0) {
				for (var i = 0; i < list.length; i++) {
					var interest = list[i];
					var im = "<img src=" + interest.value2
							+ " style='height: 420px;width: 240px;'>";
					$("#im").append(im);
				}
			}

		}
	});
}

function isflopen(id){
	var ms;
	$.ajax({
		url : _path + "/live/isflopen?id=" + id,
		async : false,
		success : function(data) {
			ms=data.msg;
		
		}
	});
	if (ms>0) {
		
		  return ms ;
		}else{
			  return ms;
		}
}
